package com.Renting.service.impl;

import com.Renting.dao.AdminDao;
import com.Renting.dao.GainDao;
import com.Renting.domain.Renting;
import com.Renting.domain.RentingUser;
import com.Renting.service.ServiceAdmin;
import com.Renting.service.ServiceGain;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

@Service
public class ServiceAdminImpl implements ServiceAdmin {

    @Resource
    AdminDao adminDao;
    @Resource
    ServiceGain gain;
    @Resource
    GainDao dao;

    ////获取所有房间的信息
    @Override
    public void AdminGetRoom(HttpSession session, Renting renting) {
        Renting ren= adminDao.AdminGetRoom(renting);
        session.setAttribute("AdminRoom",ren);
    }

    //获取所有用户信息
    @Override
    public void AdminGetUser(HttpSession session, RentingUser rentingUser) {
        RentingUser user= adminDao.AdminGetUser(rentingUser);
        session.setAttribute("AdminUser",user);
    }

    //修改房间信息
    @Override
    public void AdminAlterRoom(HttpSession session, Renting renting) {
        Renting ren= (Renting) session.getAttribute("AdminRoom");
        renting.setRid(ren.getRid());
        int i= adminDao.AdminAlterRoom(renting);
        if(i==1){
            gain.GetRoomUser(session);
            session.setAttribute("AdminHint","修改成功!");
        }else {
            session.setAttribute("AdminHint","修改失败，房间已出租!");
        }
    }

    //修改用户
    @Override
    public void AdminAlterUser(HttpSession session, RentingUser rentingUser) {
       RentingUser user=(RentingUser) session.getAttribute("AdminUser");
       rentingUser.setPid(user.getPid());
       int result= adminDao.AdminAlterUser(rentingUser);
       if(result==1){
           gain.GetRoomUser(session);
           session.setAttribute("AdminHint","修改成功!");
       }else{
           session.setAttribute("AdminHint","修改失败!");
       }

    }

    //删除房间
    @Override
    public void AdminDeleteRoom(Renting renting,HttpSession session) {
        int result= adminDao.AdminDeleteRoom(renting);
        if(result==1){
            gain.GetRoomUser(session);
            session.setAttribute("AdminHint","删除成功!");
        }else{
            session.setAttribute("AdminHint","删除失败!房间已出租!");
        }
    }

    //删除用户
    @Override
    public void AdminDeleteUser(RentingUser rentingUser,HttpSession session) {
        int result=adminDao.AdminDeleteUser(rentingUser);
        if(result==1){
            gain.GetRoomUser(session);
            session.setAttribute("AdminHint","删除成功!");
        }else{
            session.setAttribute("AdminHint","删除失败!");
        }
    }
}
